package  
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author me
	 */
	
	public class ClEnemy extends ClObject 
	{
		//public var Behavior:EnemyBehavior = EnemyBehavior.LINE;
		
		public static var SpeedRate:Number = 1;
		public static var EnemyArray:Array = new Array();
		public static var ShowBehavior:Boolean = false;
		
		public static const ENBEH_LINE:int = 0;
		public static const ENBEH_OVAL:int = 1;
		public static const ENBEH_LOOP:int = 2;
		public static const ENBEG_count:int = 3;
		public var Behavior:int = 0;
		
		public var canFire:Boolean = true;
		public var fireTimer:Timer;
		
		public var targetObject:ClObject = null;
		
		public var tmrChangeBehavior:Timer;
		
		public var betNum:TextField; 
		
		public var firePer:int = 3000;
		
		
		public function ClEnemy(stRef:Main) 
		{
			super(stRef.stage);
			
			if (Main.useEmbededContent)
				LoadEmbededImage(Assets.EnemyImage);
			else
				LoadImage("../content/images/enemy.png");
			
			EnemyArray.push(this);
			
			//addEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
		}
				
		public static function GetRandBehavior():int
		{
			return Math.round(Math.random()*(ENBEG_count-1));
		}
		
		/* Рандомное поведение */
		public function SetBehaviorRandom():void
		{
			//Behavior = GetRandBehavior();
			if (Math.random() > 0.5)
				tSpeed = 0.05;
			else
				tSpeed = -0.05;
		}
		
		public function hanChangeBehavior(e:TimerEvent):void
		{
			SetBehaviorRandom();
			tmrChangeBehavior.delay = (firePer);
		}
		
		/* При получении удара */
		public function TakeHit():void
		{
			hp--;
		}
		
		//protected function RemoveFromArray(e:Event):void
		protected function RemoveFromArray():void
		{
			EnemyArray.splice(EnemyArray.indexOf(this), 1);
		}
		
		private var t:Number = 0;
		/* Приращение t - или + */
		private var tSpeed:Number = 0;
		override protected function InitParams():void 
		{
			super.InitParams();
			
			tmrChangeBehavior = new Timer(Math.random()*3000+1000);
			tmrChangeBehavior.addEventListener(TimerEvent.TIMER, hanChangeBehavior, false, 0, true);
			tmrChangeBehavior.start();
			
			/* Номер поведения. !Лучше сделать класс для работы с текстом */
			if (ShowBehavior)
			{
				var txtFormat:TextFormat = new TextFormat();
				txtFormat.size = 20;
				betNum = new TextField();
				addChild(betNum);
				betNum.defaultTextFormat = txtFormat;
				betNum.x = 10;
				betNum.y = 10;
				betNum.wordWrap = false;
				betNum.text = Behavior.toString();
				betNum.textColor = 0xFF9999;
			}
			
			x = Math.random() * stageRef.stageWidth;
			hp = 2;
			
			/* Найдём t в текущем положении x */
			t = Math.acos(((x + x) / (stageRef.stageWidth)) - 1);
			y = Math.sin(t) * 50 + 100;
			
			if (Math.random() > 0.5)
				tSpeed = 0.05;
			else
				tSpeed = -0.05;
			
			fireTimer = new Timer(Math.random()*3000+1000, 1);
			fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
			fireTimer.start();
		}
		
		/* Обработка таймера выстрелов */
		private function fireTimerHandler(e:TimerEvent):void
		{
			//trace("Enemy fire");
			canFire = true;
		}
		
		private function doFire():void
		{
			if (canFire)
			{
				var sh1:ClEnemyMissle = new ClEnemyMissle(this);
				sh1.x = this.x; //- (this.width / 2) + 15; //+ this.xSpeed*2;
				sh1.y = this.y + 20;
				sh1.speedPower = 30;
				sh1 = null;
				canFire = false;
				fireTimer.start();
			}
		}
		
		override public function removeSelf():void
		{
			super.removeSelf();
			new ClExplosion(this);
			RemoveFromArray();
			/* Удаление кастомным обработчиков */
			fireTimer.removeEventListener(TimerEvent.TIMER, fireTimerHandler);
			tmrChangeBehavior.removeEventListener(TimerEvent.TIMER, hanChangeBehavior);
			this.removeEventListener(Event.REMOVED_FROM_STAGE, RemoveFromArray);
			targetObject = null;
		}
		
		override protected function update(e:Event):void 
		{
			//if (isDead)	return;
			
			if (ShowBehavior)
				betNum.text = Behavior.toString();
			
			if (Math.abs(x - targetObject.x) < 20)
				doFire();
			
			t += tSpeed*SpeedRate;
			if (Math.abs(t) > 2 * Math.PI)
				t = 0;
			
			switch(Behavior)
			{
				case ENBEH_LINE:
				x = (Math.cos(t) + 1) * stageRef.stageWidth / 2;
				//y = Math.sin(t)*100+100;
				break;
				case ENBEH_OVAL:
				x = (Math.cos(t) + 1) * stageRef.stageWidth / 2;
				y = Math.sin(t)*50+100;
				break;
				case ENBEH_LOOP:
				x = (Math.cos(t) + 1) * stageRef.stageWidth / 2;
				y = Math.sin(t*2)*50+100;
				break;
			}
			
			if (hp < 1)
			{
				removeSelf();
				return;
			}
		}
		
		
	}

}